

bool isValid(char* s) {
    ST st;
    STinit(&st);
    while (*s)
    {
        if (*s == '(' || *s == '{' || *s == '[')
        {
            STpush(&st, *s);
        }
        else
        {
            if (STempty(&st))
            {
                STDestroy(&st);
                return false;
            }

            char top = STTop(&st);
            STpop(&st);

            if ((*s == ')' && top != '(') ||
                (*s == ']' && top != '[') ||
                (*s == '}' && top != '{'))
            {
                STDestroy(&st);
                return false;
            }
        }
        ++s;
    }

    bool ret = STempty(&st);
    STDestroy(&st);
    return ret;
